package com.though.logistics.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class LogisticsConfig {
	
	private static final Logger logger = LoggerFactory.getLogger(LogisticsConfig.class);
	
	private static final String CONFIG = "/logistics-config.properties";

	private static final String CONFIG_TEMPLATE = "/logistics-config-template.properties";
	
	private static volatile ConfigParams configParams;
	
	static {
		InputStream in = LogisticsConfig.class.getResourceAsStream(CONFIG);
		if (in == null) {
			in = LogisticsConfig.class.getResourceAsStream(CONFIG_TEMPLATE);
			if (in == null)
				throw new RuntimeException("logistics-config.properties is not config");
		}
		Properties properties = new Properties();
		try {
			properties.load(in);
			ConfigParams params = new ConfigParams();
			params.setApikey(properties.getProperty("apikey"));
			params.setCallback(properties.getProperty("callback"));
			params.setGatway(properties.getProperty("gatway"));
			LogisticsConfig.configParams = params;
		} catch (IOException e) {
			throw new RuntimeException(e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					logger.error("Error:close logistics-config properties file", e);
				}
			}
		}
	}

	public static ConfigParams getConfigParams() {
		return configParams;
	}
}
